package com.wanmait.baohan.controller.dailyPlanController;

import com.github.pagehelper.PageInfo;

import com.wanmait.baohan.pojo.DailyPlan;
import com.wanmait.baohan.pojo.DelPlan;
import com.wanmait.baohan.pojo.PlanPic;
import com.wanmait.baohan.pojo.Staff;
import com.wanmait.baohan.service.DailyPlanService;
import com.wanmait.baohan.vo.DailyPlanVo;
import com.wanmait.baohan.vo.DataVo;

import org.springframework.stereotype.Controller;

import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 * 王森明
 *
 */
@Controller
@RequestMapping("dailyplan")
public class DailyPlanController {

    @Resource
    private DailyPlanService dailyPlanSercive;



    @RequestMapping("addPlan")
    public void addPlan(){

    }
    @RequestMapping("arrangePlan")
    public void arrangePlan(){

    }
    @RequestMapping("dayPlan")
    public void dayPlan(){

    }

    //日计划
    @RequestMapping("getDayPlan")
    @ResponseBody
    public DataVo<DailyPlanVo> getDayPlan(String date,  Integer page, Integer limit,Integer interest,Integer sid){
        DataVo<DailyPlanVo> dataVo=new DataVo<>();

        PageInfo<DailyPlan> dayList = dailyPlanSercive.findDayList(date, sid,page,limit,interest);
        List<DailyPlanVo> list1 =new ArrayList<>();
        DailyPlanVo dailyPlanVo =null;

        SimpleDateFormat simpleDateFormat1 =new SimpleDateFormat("yyyyMMdd");

        for (DailyPlan dailyPlan : dayList.getList()) {
            dailyPlanVo =new DailyPlanVo();

            dailyPlanVo.setId(dailyPlan.getId());
            dailyPlanVo.setStaffId(dailyPlan.getStaff().getId());
            dailyPlanVo.setWork(dailyPlan.getWork());
            dailyPlanVo.setStartTime(Integer.parseInt(simpleDateFormat1.format(dailyPlan.getStartTime())));
            dailyPlanVo.setEndTime(Integer.parseInt(simpleDateFormat1.format(dailyPlan.getEndTime())));
            dailyPlanVo.setTitle(dailyPlan.getTitle());
            dailyPlanVo.setAssignedById(dailyPlan.getAssignedById());
            dailyPlanVo.setPriority(dailyPlan.getPriority());
            dailyPlanVo.setEnable(dailyPlan.getEnable());
            dailyPlanVo.setModel(dailyPlan.getEnable());

            list1.add(dailyPlanVo);
        }
        dataVo.setData(list1);
        dataVo.setCount((dayList.getTotal()));
        return  dataVo;
    }


    @RequestMapping("calendar")
    public void calendar(){

    }
    //日历
    @RequestMapping("plans")
    @ResponseBody
    public DataVo plans(Integer year, Integer month,HttpServletRequest request,Integer id){


        List<DailyPlan> list = dailyPlanSercive.findEnableList(id,year ,month);
        DataVo<DailyPlanVo> dataVo =new DataVo();
        dataVo.setCode(200);
        dataVo.setMsg("1231456");

        SimpleDateFormat simpleDateFormat =new SimpleDateFormat("yyyyMMdd");
        //转成vo 应该在Service
        List<DailyPlanVo> list1 =new ArrayList<>();
        DailyPlanVo dailyPlanVo =null;

        for (DailyPlan dailyPlan : list) {
            dailyPlanVo =new DailyPlanVo();
            dailyPlanVo.setId(dailyPlan.getId());
            dailyPlanVo.setStaffId(dailyPlan.getStaff().getId());
            dailyPlanVo.setWork(dailyPlan.getWork());
            dailyPlanVo.setStartTime(Integer.parseInt(simpleDateFormat.format(dailyPlan.getStartTime())));
            dailyPlanVo.setEndTime(Integer.parseInt(simpleDateFormat.format(dailyPlan.getEndTime())));
            dailyPlanVo.setTitle(dailyPlan.getTitle());
            dailyPlanVo.setAssignedById(dailyPlan.getAssignedById());
            dailyPlanVo.setEnable(dailyPlan.getEnable());
            dailyPlanVo.setModel(dailyPlan.getEnable());

            list1.add(dailyPlanVo);
        }

        dataVo.setData(list1);
        return  dataVo;
    }

    //添加计划
    @RequestMapping("getdata")
    @ResponseBody
    public void data(HttpServletRequest request, String sid, String work, String title , String priority, String startTime, String endTime){

        HttpSession session = request.getSession();

        Staff staff =(Staff)session.getAttribute("user");
        DailyPlan dailyPlan =new DailyPlan();

        if(Integer.parseInt(sid)==staff.getId()){
            dailyPlan.setStaff(staff);
        }else{
            Staff staff2 =new Staff();
            staff2.setId(Integer.parseInt(sid));
            dailyPlan.setStaff(staff2);
            dailyPlan.setAssignedById(staff.getId());
        }


        dailyPlan.setWork(work);
        dailyPlan.setTitle(title);
        dailyPlan.setPriority(Integer.parseInt(priority));
        try {
            dailyPlan.setStartTime(new SimpleDateFormat("yyyy-MM-dd").parse(startTime));
            dailyPlan.setEndTime(new SimpleDateFormat("yyyy-MM-dd").parse(endTime));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        dailyPlanSercive.insertPlan(dailyPlan);
    }

    //删除
    @RequestMapping("delplan")
    @ResponseBody
    public void delPlan(Integer id,String value,HttpServletRequest request){
        Staff staff =(Staff)request.getSession().getAttribute("user");

        dailyPlanSercive.delPlan(id,value,staff);
    }

    //完成
    @RequestMapping("comleteplan")
    @ResponseBody
    public void comletePlan(Integer id){

        dailyPlanSercive.comletePlan(id);
    }

    //编辑
    @RequestMapping("changeplan")
    @ResponseBody
    public void changePlan(Integer id,String work){

        dailyPlanSercive.changePlan(id,work);
    }

    //查看我安排的任务
    @RequestMapping("arrangePlans")
    @ResponseBody
    public DataVo<DailyPlanVo> arrangePlan( HttpServletRequest request,String date,  Integer page, Integer limit,Integer enable){
        HttpSession session =request.getSession();

        Staff staff =(Staff)session.getAttribute("user");
        PageInfo<DailyPlan> arrangePlan = dailyPlanSercive.findArrangePlan(date, page, limit, enable, staff.getId());

        DataVo<DailyPlanVo>  dataVo =new DataVo();

        List<DailyPlanVo> list =new ArrayList<>();
        DailyPlanVo dailyPlanVo =null;

        SimpleDateFormat simpleDateFormat1 =new SimpleDateFormat("yyyyMMdd");

        for (DailyPlan dailyPlan : arrangePlan.getList()) {
            dailyPlanVo =new DailyPlanVo();

            dailyPlanVo.setId(dailyPlan.getId());
            dailyPlanVo.setStaffId(dailyPlan.getStaff().getId());
            dailyPlanVo.setWork(dailyPlan.getWork());
            dailyPlanVo.setStartTime(Integer.parseInt(simpleDateFormat1.format(dailyPlan.getStartTime())));
            dailyPlanVo.setEndTime(Integer.parseInt(simpleDateFormat1.format(dailyPlan.getEndTime())));
            dailyPlanVo.setTitle(dailyPlan.getTitle());
            dailyPlanVo.setAssignedById(dailyPlan.getAssignedById());
            dailyPlanVo.setPriority(dailyPlan.getPriority());
            dailyPlanVo.setEnable(dailyPlan.getEnable());
            dailyPlanVo.setModel(dailyPlan.getEnable());
            dailyPlanVo.setStaff(dailyPlan.getStaff().getName());

            list.add(dailyPlanVo);
        }

        dataVo.setData(list);
        dataVo.setCount((arrangePlan.getTotal()));
        return  dataVo;
    }
    //iframe 完成计划 completePlan.html
    @RequestMapping("completePlan")
    public void completePlan(){

    }
    //完成计划
    @PostMapping("completePlan")
    public void completePlans(MultipartFile[] file,Integer id){

        dailyPlanSercive.inserPlanPic(file,id);

    }

    //转到计划完成的iframe
    @RequestMapping("planPictures")
    public ModelAndView planPictures(Integer id){

        ModelAndView modelAndView =new ModelAndView();
        List<PlanPic> pictures = dailyPlanSercive.findPlanPicturesById(id);
        modelAndView.addObject("pictures",pictures);


        return modelAndView;
    }
    //删除的详情
    @RequestMapping("delReason")
    public ModelAndView delReason(Integer id){
        ModelAndView modelAndView =new ModelAndView();
        DelPlan delplanDetail = dailyPlanSercive.findDelplanDetail(id);

        System.out.println(delplanDetail);
        System.out.println("-----------------------");
        modelAndView.addObject("del",delplanDetail);
        return  modelAndView;
    }
}
